package br.com.jorganizer.model.filterrule.simple;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.util.StringUtils;

import br.com.jorganizer.model.filterrule.FilterRule;

/**
 * A rule with a simples asteriks
 * Example:
 * <ul>
 * 	<li>*.mp3</li>
 * 	<li>*.doc</li>
 *  <li>movie*.rmvb</li>
 *  <li>*java*installer*.exe</li>
 * </ul>
 * 
 * @author Luciano
 *
 */

public class SimpleRule implements FilterRule {

	private String asteriskExpression;


	public SimpleRule(String asteriskExpression) {
		
		if (asteriskExpression == null)
			throw new NullPointerException();
		
		if (asteriskExpression.isEmpty())
			throw new IllegalArgumentException("SimpleExpression must be diferrent empty");

		this.asteriskExpression = asteriskExpression;
	}

	public boolean accepted(String fileName) {

		String[] parts = asteriskExpression.split("\\*");
		String regexExpression;

		if (parts.length == 0)
			regexExpression = (fileName);
		else
			regexExpression = StringUtils.arrayToDelimitedString(parts, ".*");

		Pattern p = Pattern.compile(regexExpression, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(fileName);

		return m.matches();
	}

}
